<?php

namespace app\api\controller;

use app\common\BaseHelper;

use app\admin\model\CouponProduct;
use app\admin\model\CouponProductCategory;
use app\admin\model\Membercoupon;
use app\admin\model\Productcategory;
use think\Request;

class Coupon extends Base
{

    /**
     * 领取优惠券
     */
    public function receive()
    {
        $param = self::getHttpParam();

        if (empty($param->memberId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '会员ID(memberId) 不能为空');
        }
        if (empty($param->couponId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '优惠券ID(couponId) 不能为空');
        }

        $where = [
            'member_id' => $param->memberId,
            'coupon_id' => $param->couponId,
        ];
        $model = new Membercoupon();
        $item = $model->where($where)->field('id')->find();
        if ($item) {
            return show(config('status.ERROR_STATUS'), '您已领取过', '您已领取过');
        }

        $data = [
            'id' => BaseHelper::getUUID(),
            'create_date' => date('Y-m-d H:i:s'),
            'member_id' => $param->memberId,
            'coupon_id' => $param->couponId,
            'status' => 1
        ];
        $result = $model->insert($data);
        if ($result === false) {
            return show(config('status.ERROR_STATUS'), '领取失败', '领取失败');
        }

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $result);
    }

    //获取订单可用优惠券
    public function getOrderCouponList(Request $request){
        $param = $request->param();
        $shoppingcar = $param['shopcar'];
        $memberId = $param['memberId'];
        if(empty($shoppingcar) || empty($memberId)){
            return show(config('status.ERROR_STATUS'),'参数异常','参数异常');
        }
        $Membercoupon = Membercoupon::where(['member_id'=>$memberId,'status'=>1])->select();
        if($Membercoupon){
            $coupon_type1 = []; //全场抵现券
            $coupon_type2 = []; //品类抵现券
            $coupon_type3 = []; //单品抵现券
            $coupon_type4 = []; //全场折扣券
            $coupon_type5 = []; //品类折扣券
            $coupon_type6 = []; //单品折扣券
            foreach ($Membercoupon as $v){
                $Coupon = db('coupon')->where('id',$v['coupon_id'])->find();
                $Coupon['my_collect_id'] = $v['id'];
                $nowtime = time();
                $starttime = strtotime($Coupon['start_time']);
                $endtime = strtotime($Coupon['end_time']);
                if($nowtime > $starttime && $nowtime < $endtime){
                    if($Coupon['coupon_type'] == 1){
                        array_push($coupon_type1,$Coupon);
                    }elseif ($Coupon['coupon_type'] == 2){
                        array_push($coupon_type2,$Coupon);
                    }elseif ($Coupon['coupon_type'] == 3){
                        array_push($coupon_type3,$Coupon);
                    }
                    elseif ($Coupon['coupon_type'] == 4){
                        array_push($coupon_type4,$Coupon);
                    }
                    elseif ($Coupon['coupon_type'] == 5){
                        array_push($coupon_type5,$Coupon);
                    }else{
                        array_push($coupon_type6,$Coupon);
                    }
                }
            }
            $res1=[];$res2=[];$res3=[];$res4=[];$res5=[];$res6=[];
            if($coupon_type1){
                $res1 = $this->Full_court($coupon_type1,$shoppingcar,1);
            }
            if($coupon_type2){
                $res2 = $this->Category($coupon_type2,$shoppingcar,2);
            }
            if($coupon_type3){
                $res3 = $this->Single_product($coupon_type3,$shoppingcar,3);
            }
            if($coupon_type4){
                $res4 = $this->Full_court($coupon_type4,$shoppingcar,4);
            }
            if($coupon_type5){
                $res5 = $this->Category($coupon_type5,$shoppingcar,5);
            }
            if($coupon_type6){
                $res6 = $this->Single_product($coupon_type6,$shoppingcar,6);
            }
            $res = array_merge($res1,$res2,$res3,$res4,$res5,$res6);
            return show(config('status.SUCCESS_STATUS'),'ok',$res);
        }else{
            return show(config('status.SUCCESS_STATUS'),'暂无数据',[]);
        }
    }

    //全场
    private function Full_court($coupon_type,$shoppingcar,$type){
        $returncouponList = [];
        $allprice = 0;
        foreach ($shoppingcar as $v){
            $price = \app\admin\model\Product::where(['id'=>$v['product_id'],'type'=>0])->value('price');
            $allprice+=$price*$v['count'];
        }
        foreach ($coupon_type as $v){
            if($type == 1){
                if($allprice >= $v['threshold']){
                    array_push($returncouponList,$v);
                }
            }else{
                if($allprice > 100){
                    $v['zhekouprice'] = intval($allprice * ((100-$v['discount_strength']) / 100));
                    if($v['threshold']){
                        if($allprice >= $v['threshold']){
                            array_push($returncouponList,$v);
                        }
                    }else{
                        array_push($returncouponList,$v);
                    }
                }
            }
        }
        return $returncouponList;
    }

    //品类
    private function Category($coupon_type,$shoppingcar,$type){
        $returncouponList = [];
        $productidarr = [];
        foreach ($shoppingcar as $v){
            array_push($productidarr,$v['product_id']);
        }
        $Product = db('product')->where('id','in',$productidarr)->where('type',0)->field('id,category_id,price')->select();
        foreach ($shoppingcar as $v){
            foreach ($Product as &$val){
                if($v['product_id'] == $val['id']){
                    $val['count'] = $v['count'];
                }
            }
        }
        $Product = \app\common\Common::array_group_by($Product,'category_id');
        $truecate = [];
        foreach ($Product as &$value){
            $cateprice = 0;
            foreach ($value as &$item){
                $cateprice+=$item['price'] * $item['count'];
            }
            array_push($truecate,['category_id'=>$value[0]['category_id'],'cateallprice'=>$cateprice]);
        }
        foreach ($coupon_type as $v){
            $res = CouponProductCategory::where('coupon_id',$v['id'])->find();
            $catarr = explode(',',$res['category_id']);
            $category = Productcategory::where('id','in',$catarr)->select();
            $category_name = '';
            $i = 0;
            foreach ($category as $value){
                if($i == 0){
                    $category_name .= $value['name'];
                }else{
                    $category_name .= '、' . $value['name'];
                }
                $i++;
            }
            $v['category_name'] = $category_name;
            $allprice = 0;
            foreach ($truecate as $item) {
                if(in_array($item['category_id'],$catarr)){
                    $allprice += $item['cateallprice'];
                }
            }
            if($type == 2){
                if($allprice >= $v['threshold']){
                    array_push($returncouponList,$v);
                }
            }else{
                if($allprice > 100){
                    $v['zhekouprice'] = intval($allprice * ((100-$v['discount_strength']) / 100));
                    if($v['threshold']){
                        if($allprice >= $v['threshold']){
                            array_push($returncouponList,$v);
                        }
                    }else{
                        array_push($returncouponList,$v);
                    }
                }
            }
        }
        return $returncouponList;
    }

    //单品
    private function Single_product($coupon_type,$shoppingcar,$type){
        $returncouponList = [];
        $productidarr = [];
        foreach ($shoppingcar as $v){
            array_push($productidarr,$v['product_id']);
        }
        $Product = db('product')->where('id','in',$productidarr)->where('type',0)->field('id,price,name')->select();
        foreach ($shoppingcar as $v){
            foreach ($Product as &$val){
                if($v['product_id'] == $val['id']){
                    $val['count'] = $v['count'];
                }
            }
        }
        foreach ($coupon_type as $v){
            $res = CouponProduct::where('coupon_id',$v['id'])->find();
            foreach ($Product as $value){
                $allprice = $value['price'] * $value['count'];
                $v['product_name'] = $value['name'];
                if($value['id'] == $res['product_id']){
                    if($type == 3){
                        if($allprice >= $v['threshold']){
                            array_push($returncouponList,$v);
                        }
                    }else{
                        if($allprice > 100){
                            $v['zhekouprice'] = intval($allprice * ((100-$v['discount_strength']) / 100));
                            if($v['threshold']){
                                if($allprice >= $v['threshold']){
                                    array_push($returncouponList,$v);
                                }
                            }else{
                                array_push($returncouponList,$v);
                            }
                        }
                    }
                }
            }
        }
        return $returncouponList;
    }
}